package zj.com.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zfdang.multiple_images_selector.SelectorSettings;

import org.xutils.DbManager;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import zj.com.adapter.UploadImgGridAdapter;
import zj.com.db.DBUtil;
import zj.com.model.Stcj;
import zj.com.model.StcjDB;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/8.
 *
 *质量抓拍
 */
public class StcjDetailActivity extends BaseActivity implements View.OnClickListener{
    public TextView title_name,right_tv;
    public ImageView left_iv;

    public GridView uploadGridView;
    public List<String> list;
    public UploadImgGridAdapter adapter;
    public static final int REQUEST_CODE = 732;

    public  static ArrayList<String> mResults = new ArrayList<>();

    public EditText desc_et,question_et;
    public TextView gs_tv,xm_tv;

    public List<String> serverList;
    public List<String> localList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stcj_detail);

        initView();
    }

    public Stcj stcj;
    public void initView(){
        localList = new ArrayList<>();

        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("实体采集详情");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("保存");
        right_tv.setOnClickListener(this);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);

        uploadGridView = (GridView) findViewById(R.id.uploadGridView);

        list = new ArrayList<>();
        adapter = new UploadImgGridAdapter(list,this);
        uploadGridView.setAdapter(adapter);

        desc_et = (EditText) findViewById(R.id.desc_et);
        question_et = (EditText) findViewById(R.id.question_et);
        xm_tv = (TextView) findViewById(R.id.xm_tv);
        gs_tv = (TextView) findViewById(R.id.gs_tv);



        stcj= (Stcj) getIntent().getSerializableExtra("stcj");
        if(stcj != null){

            gs_tv.setText(stcj.getUnitName());
            xm_tv.setText(stcj.getProjectName());
            question_et.setText(stcj.getInfoName());
            desc_et.setText(stcj.getInfoRemark());

            serverList = stcj.getAttachList();
            if(serverList != null && serverList.size() > 0){
                list.addAll(serverList);
            }
            list.add("");
            adapter.setDataChange(list);
        }else{

             stcjdb = (StcjDB) getIntent().getSerializableExtra("stcjdb");
          if(stcjdb != null){

              stcj = new Stcj();
              stcj.setInfoId(stcjdb.getInfoId());
              stcj.setProjectName(stcjdb.getProjectName());
              stcj.setDeptName(stcjdb.getDeptName());
              stcj.setFinishedDate(stcjdb.getFinishedDate());
              stcj.setInfoName(stcjdb.getInfoName());
              stcj.setInfoRemark(stcjdb.getInfoRemark());
              stcj.setStatus(stcjdb.getStatus());
              stcj.setUnitName(stcjdb.getUnitName());


              String img = stcjdb.getAttachList();
              localList = new ArrayList<>();
              if(img != null){
                 String[] imgs =  img.split(",");
                  for(int i=0;i<imgs.length;i++){
                      localList.add(imgs[i]);
                  }
                  list.addAll(localList);
              }
              gs_tv.setText(stcj.getUnitName());
              xm_tv.setText(stcj.getProjectName());
              question_et.setText(stcj.getInfoName());
              desc_et.setText(stcj.getInfoRemark());
              list.add("");
              adapter.setDataChange(list);

          }
        }
    }
   public  StcjDB   stcjdb;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // get selected images from selector
        if(requestCode == REQUEST_CODE) {
            if(resultCode == RESULT_OK) {
                mResults = data.getStringArrayListExtra(SelectorSettings.SELECTOR_RESULTS);
                assert mResults != null;

                // show results in textview
                StringBuilder sb = new StringBuilder();
                sb.append(String.format("Totally %d images selected:", mResults.size())).append("\n");
                list.clear();
                localList.clear();
                for(String result : mResults) {
                  //  list.add(result);
                    sb.append(result).append("\n");
                    localList.add(result);

                }
                Log.e("wxf", sb.toString());

                list.addAll(localList);
                list.add("");
                adapter.setDataChange(list);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                save();
                break;
        }
    }

    public  void save(){


     String desc =    desc_et.getText().toString();
     if(TextUtils.isEmpty(desc)){


         Toast.makeText(StcjDetailActivity.this, "描述不能为空", Toast.LENGTH_SHORT).show();
         return;
     }

     if(localList == null || localList.size() == 0){
         Toast.makeText(StcjDetailActivity.this, "请添加本地图片", Toast.LENGTH_SHORT).show();

         return;
     }

        StringBuffer sb = new StringBuffer();
        for(String str:localList){

            sb.append(str);
            sb.append(",");
        }
        StcjDB db = new StcjDB();
        db.setInfoId(stcj.getInfoId());
        db.setProjectName(stcj.getProjectName());
        db.setDeptName(stcj.getDeptName());
        db.setFinishedDate(stcj.getFinishedDate());
        if(stcjdb != null){
            db.setId(stcjdb.getId());
            db.setMode(1);
        }else{
            db.setId(UUID.randomUUID().toString());

        }
        db.setInfoName(stcj.getInfoName());
        db.setInfoRemark(desc);
        db.setStatus(stcj.getStatus());
        db.setUnitName(stcj.getUnitName());
        db.setAttachList(sb.toString());

        try {
            DBUtil.getInstance(this).getDBManager().saveOrUpdate(db);
            Toast.makeText(StcjDetailActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
            finish();
        }catch (Exception e){

            LogUtils.getInstance().error(e.toString());
        }

    }
}
